package com.cinsea.ext;

import com.alibaba.fastjson.JSON;
import com.cinsea.common.Env;
import com.cinsea.common.ExtAction;
import com.cinsea.common.JdbcDao;
import com.cinsea.common.Page;
import com.cinsea.file.model.Filestore;
import com.cinsea.file.service.FilestoreService;
import com.cinsea.hrm.service.HumanService;
import com.cinsea.util.NumberUtil;
import com.cinsea.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ProjectAction extends ExtAction {
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String action = StringUtil.null2String(request.getParameter("action"));
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        HumanService humanService = Env.getBean("humanService");
        FilestoreService filestoreService=Env.getBean("filestoreService");
        if (action.equals("getreply")) {
            String gtname = StringUtil.trimToNull(request.getParameter("gtname"));
            int pageNo = NumberUtil.string2Int(request.getParameter("pageno"), 1);
            int pageSize = NumberUtil.string2Int(request.getParameter("pagesize"), 20);
            if (!StringUtil.isEmpty(request.getParameter("start")))
                pageNo = NumberUtil.string2Int(request.getParameter("start"), 0) / pageSize + 1;

            String sql = "select processid,content,name,dates,time,attach,gtname from ut_feedback where 1=1";
            if (!StringUtil.isEmpty(gtname)) {
                sql += " and gtname='" + gtname + "'";
            }
            sql += " order by dates desc,time desc";
            Page pageObject = jdbcDao.pagedQuery(sql, pageNo, pageSize);


            List list = new ArrayList();
            if (pageObject.getTotalSize() != 0) {
                list = (List) pageObject.getResult();
            }
            List arrayjson = new ArrayList();


            for (Object row : list) {
                Map object = new HashMap();
                String processid = (String) ((Map) row).get("processid");
                object.put("processid", processid);
                String userid = (String) ((Map) row).get("name");
                String name = humanService.get(userid).getObjname();
                object.put("name", name);
                String content = (String) ((Map) row).get("content");
                object.put("content", content);
                String dates = (String) ((Map) row).get("dates");
                object.put("dates", dates);
                String time = (String) ((Map) row).get("time");
                object.put("time", time);
			    String attach = (String) ((Map) row).get("attach");
                if (!StringUtil.isEmpty(attach)) {
                    List<String> attchlist = StringUtil.string2ArrayList(attach, ",");
                    String filenames = "";
                    for (String attachid : attchlist) {
                        Filestore filestore = filestoreService.get(attach);
                        filenames += "<a href='/filedownload.do?attachid=" + attachid + "&download=1'>" + filestore.getObjname() + "</a>&nbsp;";
                    }
                    object.put("attach", filenames);
                }
                arrayjson.add(object);
            }//end for
            Map jsonresult = new HashMap();
            jsonresult.put("totalcount", pageObject.getTotalSize());
            jsonresult.put("result", arrayjson);
            response.getWriter().print(JSON.toJSONString(jsonresult));
            return;
        }
    }
}
